package com.ouc.eddyAlgorithm.controller;

import com.ouc.common.annotation.Log;
import com.ouc.common.config.OUCAjaxResult;
import com.ouc.common.core.controller.BaseController;
import com.ouc.common.core.domain.AjaxResult;
import com.ouc.common.core.page.TableDataInfo;
import com.ouc.common.enums.BusinessType;
import com.ouc.common.utils.excel.ExcelUtils;
import com.ouc.common.utils.file.FileUtils;
import com.ouc.common.utils.poi.ExcelUtil;
import com.ouc.eddyAlgorithm.domain.VgA4b3Track;
import com.ouc.eddyAlgorithm.service.IVgA4b3TrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.*;

/**
 * 涡旋跟踪Controller
 *
 * @author ouc
 * @date 2024-08-10
 */
@RestController
@RequestMapping("/eddyAlgorithm/track")
public class VgA4b3TrackController extends BaseController {
  @Autowired
  private IVgA4b3TrackService vgA4b3TrackService;

  /**
   * 查询涡旋跟踪列表
   */
//  @PreAuthorize("@ss.hasPermi('eddyAlgorithm:track:list')")
  @GetMapping("/list")
  public TableDataInfo list(VgA4b3Track vgA4b3Track) {
    startPage();
    List<VgA4b3Track> list = vgA4b3TrackService.selectVgA4b3TrackList(vgA4b3Track);
    return getDataTable(list);
  }

  /**
   * 导出涡旋跟踪列表
   */
//  @PreAuthorize("@ss.hasPermi('eddyAlgorithm:track:export')")
  @Log(title = "涡旋跟踪", businessType = BusinessType.EXPORT)
  @GetMapping("/export")
  public void export(HttpServletResponse response, VgA4b3Track vgA4b3Track) {
    List<VgA4b3Track> list = vgA4b3TrackService.selectVgA4b3TrackList(vgA4b3Track);
    ExcelUtil<VgA4b3Track> util = new ExcelUtil<VgA4b3Track>(VgA4b3Track.class);
    util.exportExcel(response, list, "涡旋跟踪数据");
  }

  /**
   * 获取涡旋跟踪详细信息
   */
  @PreAuthorize("@ss.hasPermi('eddyAlgorithm:track:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Integer id) {
    return success(vgA4b3TrackService.selectVgA4b3TrackById(id));
  }

  /**
   * 新增涡旋跟踪
   */
  @PreAuthorize("@ss.hasPermi('eddyAlgorithm:track:add')")
  @Log(title = "涡旋跟踪", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody VgA4b3Track vgA4b3Track) {
    return toAjax(vgA4b3TrackService.insertVgA4b3Track(vgA4b3Track));
  }

  /**
   * 修改涡旋跟踪
   */
  @PreAuthorize("@ss.hasPermi('eddyAlgorithm:track:edit')")
  @Log(title = "涡旋跟踪", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody VgA4b3Track vgA4b3Track) {
    return toAjax(vgA4b3TrackService.updateVgA4b3Track(vgA4b3Track));
  }

  /**
   * 删除涡旋跟踪
   */
  @PreAuthorize("@ss.hasPermi('eddyAlgorithm:track:remove')")
  @Log(title = "涡旋跟踪", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Integer[] ids) {
    return toAjax(vgA4b3TrackService.deleteVgA4b3TrackByIds(ids));
  }

  @GetMapping("updateEddy")
  public AjaxResult updateEddy(@RequestParam(value = "startTime") String startTime,
                               @RequestParam(value = "endTime") String endTime,
                               @RequestParam(value = "startLat", required = false) Float startLat,
                               @RequestParam(value = "endLat", required = false) Float endLat,
                               @RequestParam(value = "startLon", required = false) Float startLon,
                               @RequestParam(value = "endLon", required = false) Float endLon,
                               @RequestParam(value = "trackTime") int trackTime,
                               @RequestParam(value = "trackLevel") int trackLevel,
                               @RequestParam(value = "trackNum") float trackNum) throws ParseException {
    int num = vgA4b3TrackService.vgEddyTrack(startTime, endTime, startLat, endLat, startLon, endLon, trackTime, trackLevel, trackNum);
    System.out.println("更新涡旋数目num = " + num);
    return AjaxResult.success(num);
  }

  /**
   * 查询涡旋跟踪列表(预报数据，1天)
   * <p>
   * time 2025-01-01
   */

  @GetMapping("/listByForecast")
  public AjaxResult listByForecast(@RequestParam(value = "time") String time) {

    VgA4b3Track vgA4b3Track = new VgA4b3Track();
    String tableName = "vg_forecast";

    Map<String, Object> params = new HashMap<>();
    params.put("tableName", tableName);
    params.put("time", time);
    vgA4b3Track.setParams(params);
    List<VgA4b3Track> list = vgA4b3TrackService.selectVgA4b3TrackByForecast(vgA4b3Track);
    return AjaxResult.success(list);
  }

  /**
   * 将EXCEL表单中的数据导入到vg数据表中
   */
  @ResponseBody
  @RequestMapping("/putExcelToMysql")
  public AjaxResult putExcelToMysql(@RequestParam(value = "filePath") String filePath) throws Exception {
    try {
      File excelFile = new File(filePath);
      List<VgA4b3Track> vgA4b3TrackList = ExcelUtils.readFile(excelFile, VgA4b3Track.class);
      for (VgA4b3Track vgA4b3Track : vgA4b3TrackList) {
        vgA4b3TrackService.insertVgA4b3Track(vgA4b3Track);
      }
      return AjaxResult.success("文件已通过有效性和规范性检查，已导入涡旋数据=" + vgA4b3TrackList.size());
    }catch (Exception e){
      return AjaxResult.error("文件未通过有效性和规范性检查，无法插入数据库。");
    }

  }

  /**
   * 涡旋数据统计
   */
  @ResponseBody
  @RequestMapping("/dataStatistics")
  public OUCAjaxResult dataStatistics() {
    Map<String, Integer> result = new HashMap<>();
    List<VgA4b3Track> list = vgA4b3TrackService.selectVgA4b3TrackList(null);
    result.put("涡旋个数", list.size());

    Integer isAnomalous = 0;
    for (VgA4b3Track eddy : list) {
      if(eddy.getIsAnomalous()==1) isAnomalous++;
    }

    return OUCAjaxResult.ok(result);
  }
}
